package com.huike.web.controller.clues;

import com.huike.clues.domain.TbClue;
import com.huike.clues.domain.TbClueTrackRecord;
import com.huike.clues.domain.vo.ClueTrackRecordVO;

import com.huike.clues.service.ITbCluesRecordService;
import com.huike.clues.service.impl.TbCluesRecordServiceImpl;
import com.huike.common.core.controller.BaseController;
import com.huike.common.core.domain.AjaxResult;
import com.huike.common.core.page.TableDataInfo;
import com.huike.common.utils.DateUtils;
import com.huike.common.utils.SecurityUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 线索跟进Controller
 * @Author: Rodgers
 * @Date 2022/3/31 11:41
 */
@RestController
@RequestMapping("/clues/record")
public class TbCluesRecordController extends BaseController {

    @Autowired(required = false)
    private ITbCluesRecordService iTbCluesRecordService;

    /**
     * 查询线索跟进记录列表
     * @param clueId
     * @return
     */
    @GetMapping("/list")
    public TableDataInfo list(@RequestParam("clueId")Long clueId){
        startPage();
        List<TbClueTrackRecord> list = iTbCluesRecordService.selectCluesRecordList(clueId);
        return getDataTable(list);
    }

    @PostMapping
    public AjaxResult add(@RequestBody ClueTrackRecordVO clueTrackRecordVo){
        //新构建一个插入对象
        TbClueTrackRecord trackRecord = new TbClueTrackRecord();
        //将传递过来的数据封装进插入对象里
        BeanUtils.copyProperties(clueTrackRecordVo,trackRecord);
        //设置修改人
        trackRecord.setCreateBy(SecurityUtils.getUsername());
        //设置创建时间
        trackRecord.setCreateTime(DateUtils.getNowDate());

        //新构建一个线索对象
        TbClue tbClue = new TbClue();
        //设置状态为  3:跟进中
        tbClue.setStatus(TbClue.StatusType.FOLLOWING.getValue());
        //设置id
        tbClue.setId(clueTrackRecordVo.getClueId());

        return AjaxResult.success(iTbCluesRecordService.add(trackRecord,tbClue));
    }
}
